package com.foreveross.mofang.datapersistence.application.vo;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.validation.constraints.NotNull;

/**
 * 数据文件相关信息vo
 * 
 * @author fb
 */
public class FileInfo extends BaseVo {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@NotNull(message = "原文件名称不能空!")
	private String originalName;
	
	@NotNull(message = "文件大小不能空!")
	private Long fileSize;
	
	@NotNull(message = "md5校验和不能空!")
	private String md5;
	
	@NotNull(message = "文件下载地址不能为空!")
	private String downloadUrl;
	
	@NotNull(message = "文件创建时间不能为空!")
	private String createDate;
	
	public FileInfo() {
		
	}
	
	public FileInfo(String originalName, Long fileSize, Long createDate) {
		super();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		Date date = new Date();
		date.setTime(createDate);
		this.originalName = originalName;
		this.fileSize = fileSize;
		this.createDate = sdf.format(date);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((createDate == null) ? 0 : createDate.hashCode());
		result = prime * result
				+ ((fileSize == null) ? 0 : fileSize.hashCode());
		result = prime * result
				+ ((originalName == null) ? 0 : originalName.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		FileInfo other = (FileInfo) obj;
		if (createDate == null) {
			if (other.createDate != null)
				return false;
		} else if (!createDate.equals(other.createDate))
			return false;
		if (fileSize == null) {
			if (other.fileSize != null)
				return false;
		} else if (!fileSize.equals(other.fileSize))
			return false;
		if (originalName == null) {
			if (other.originalName != null)
				return false;
		} else if (!originalName.equals(other.originalName))
			return false;
		return true;
	}
	
	public String getIp() {
		Pattern pattern = Pattern.compile("//(.*?)/");
		Matcher matcher = pattern.matcher(downloadUrl);
		matcher.find();
		return matcher.group(1); 
	}

	public String getOriginalName() {
		return originalName;
	}

	public void setOriginalName(String originalName) {
		this.originalName = originalName;
	}

	public Long getFileSize() {
		return fileSize;
	}

	public void setFileSize(Long fileSize) {
		this.fileSize = fileSize;
	}

	public String getMd5() {
		return md5;
	}

	public void setMd5(String md5) {
		this.md5 = md5;
	}

	public String getDownloadUrl() {
		return downloadUrl;
	}

	public void setDownloadUrl(String downloadUrl) {
		this.downloadUrl = downloadUrl;
	}

	public String getCreateDate() {
		return createDate;
	}

	public void setCreateDate(String createDate) {
		this.createDate = createDate;
	}

}
